import {DynamicModule, Module} from "@nestjs/common";

@Module({
    providers: [
        {
            provide: 'PREFIX',
            useValue: 'prefix',
        }
    ],
    exports: ['PREFIX'],
})
export class DynamicConfigModule {
    // 动态模块的意义，用于传参，动态创建
    static forRoot(apiKey: string = 'apiKey:00', isSync = true): DynamicModule | Promise<DynamicModule> {
        const providers = [
            {
                provide: 'CONFIG',
                useValue: {ApiKey: apiKey},
            }
        ]
        const controller = []
        const moduleInfo = {
            module: DynamicConfigModule,
            providers,
            controllers: controller,
            exports: providers.map((provider) => {
                if (provider instanceof Function) {
                    return provider;
                } else {
                    return provider.provide
                }
            })
        }
        if (isSync) {
            return moduleInfo
        } else {
            return new Promise((resolve) => {
                setTimeout(() => {
                    resolve(moduleInfo)
                }, 3000)
            })
        }
    }
}
